package cn.daniel.video.controller;

import cn.daniel.commonutils.Response;
import cn.daniel.commonutils.exceptionhandler.exception.MyException;
import cn.daniel.commonutils.result.ResultCode;
import cn.daniel.video.constants.InitAliyunClient;
import cn.daniel.video.constants.VideoConstants;
import cn.daniel.video.service.VodService;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@Api(tags = "阿里云视频服务")
@RestController
@RequestMapping("/service/video")
public class VodController {
    @Autowired
    private VodService vodService;

    @ApiOperation("上传视频到阿里云")
    @PostMapping("/uploadVideo")
    public Response uploadVideo(MultipartFile file){
        //上传的视频若为空则报错抛出异常
        Optional.ofNullable(file).orElseThrow(()-> MyException.setException(ResultCode.FILE_EMPTY));
        String videoId=vodService.uploadVideo(file);
        return Response.ok().data("videoId",videoId);
    }

    @ApiOperation("根据视频id删除阿里云视频")
    @DeleteMapping("/removeVideoById/{id}")
    public Response removeVideoById(@PathVariable String id){
        //初始化阿里云视频点播的client
        DefaultAcsClient client = InitAliyunClient.initVodClient(VideoConstants.KEY_ID, VideoConstants.KEY_SECRET);
        //获取删除视频的request对象
        DeleteVideoRequest videoRequest = new DeleteVideoRequest();
        videoRequest.setVideoIds(id); //设置要删除视频的id

        try{
            //执行删除操作
            client.getAcsResponse(videoRequest);

        }catch (Exception e){
            e.printStackTrace();
            throw MyException.setException(ResultCode.DELETE_FILE_ERROR);

        }
        return Response.ok();
    }

    @ApiOperation("删除多个阿里云视频")
    @DeleteMapping("/deleteBatchVideo")
    public Response deleteBatchVideo(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.deleteBatchVideo(videoIdList);
        return Response.ok();
    }

    @ApiOperation("根据视频id获取视频凭证")
    @GetMapping("/getPlayAuth/id")
    public Response getPlayAuth(@PathVariable String id){
        DefaultAcsClient client = InitAliyunClient.initVodClient(VideoConstants.KEY_ID, VideoConstants.KEY_SECRET);
        GetVideoPlayAuthRequest authRequest = new GetVideoPlayAuthRequest();
        authRequest.setVideoId(id);
        GetVideoPlayAuthResponse acsResponse=null;
        try{
             acsResponse = client.getAcsResponse(authRequest);

        }catch (Exception e){
            e.printStackTrace();
            throw MyException.setException(ResultCode.GET_PLAYAUTH_FAIL);
        }
        //获取播放的视频凭证
        String playAuth=acsResponse.getPlayAuth();
        return Response.ok().data("playAuth",playAuth);
    }

    /**
     * 获取上传的视频信息
     * @param videoId
     * @return
     */
    @GetMapping("/getVideoInfo/{videoId}")
    public Response getVideoInfo(@PathVariable String videoId){
        HashMap<String,Object> map=vodService.getVideoInfo(videoId);
        return Response.ok().data(map);
    }



}
